package org.jsola.hr.service.impl;

import org.jsola.common.DateKit8;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IBookingsBusinessDAO;
import org.jsola.hr.dto.BookingsBusinessAddDTO;
import org.jsola.hr.dto.BookingsBusinessUpdateDTO;
import org.jsola.hr.entity.BookingsBusinessDO;
import org.jsola.hr.entity.BookingsDatetimeDO;
import org.jsola.hr.query.BookingsBusinessQuery;
import org.jsola.hr.service.IBookingsBusinessService;
import org.jsola.hr.service.IBookingsDatetimeService;
import org.jsola.hr.vo.BookingsBusinessListVO;
import org.jsola.hr.vo.BookingsBusinessVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 预约业务主表
 *
 * @author zhr
 */
@Service("hrBookingsBusinessServiceImpl")
public class BookingsBusinessServiceImpl implements IBookingsBusinessService {

    @Autowired
    private IBookingsBusinessDAO bookingsBusinessDAO;

    @Autowired
    private IBookingsDatetimeService bookingsDatetimeService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public BookingsBusinessVO save(BookingsBusinessAddDTO bookingsBusinessAddDTO, TokenUser tokenUser) {
        // 转DO
        BookingsBusinessDO bookingsBusinessDO = bookingsBusinessAddDTO.to(BookingsBusinessDO.class);
        // 保存
        bookingsBusinessDO = save(bookingsBusinessDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return bookingsBusinessDO.to(BookingsBusinessVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(BookingsBusinessUpdateDTO bookingsBusinessUpdateDTO, TokenUser tokenUser) {
        //转DO
        BookingsBusinessDO bookingsBusinessDO = bookingsBusinessUpdateDTO.to(BookingsBusinessDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(bookingsBusinessDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... bookingsBusinessIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) bookingsBusinessIds);
    }


    @Override
    public BookingsBusinessVO selectById(Long bookingsBusinessId, String siteId) {
        BookingsBusinessDO bookingsBusinessDO = selectDOById(bookingsBusinessId, siteId);
        if (bookingsBusinessDO == null) {
            return null;
        }
        return bookingsBusinessDO.to(BookingsBusinessVO.class);
    }

    @Override
    public List<BookingsBusinessListVO> select(BookingsBusinessQuery bookingsBusinessQuery, String siteId) {
        List<BookingsBusinessDO> bookingsBusinessDOList = selectDO(bookingsBusinessQuery, siteId);
        if (CollectionUtils.isEmpty(bookingsBusinessDOList)) {
            return bookingsBusinessDOList == null ? null : new ArrayList<>();
        }
        List<BookingsBusinessListVO> bookingsBusinessList = bookingsBusinessDOList.stream()
                .map(bookingsBusinessDO -> bookingsBusinessDO.to(BookingsBusinessListVO.class))
                .collect(Collectors.toList());
        for (BookingsBusinessListVO bookingsBusinessListVO : bookingsBusinessList) {
            List<BookingsDatetimeDO> bookingsDatetimeDOList =
                    bookingsDatetimeService.selectByBookingsBusinessId(bookingsBusinessListVO.getId());
            Map<Date, List<BookingsDatetimeDO>> groupDateTime =
                    bookingsDatetimeDOList.stream().collect(Collectors.groupingBy(BookingsDatetimeDO::getBookingsDay));
            Map<String, List<BookingsDatetimeDO>> groupDateStr = new HashMap<>(128);
            for (Date date : groupDateTime.keySet()) {
                groupDateStr.put(DateKit8.format2Date(date), groupDateTime.get(date));
            }
            bookingsBusinessListVO.setBookingsDatetime(groupDateStr);
        }
        return bookingsBusinessList;
    }

    @Override
    public int selectCount(BookingsBusinessQuery bookingsBusinessQuery, String siteId) {
        Example example = buildExample(bookingsBusinessQuery, siteId);
        return bookingsBusinessDAO.selectCountByExample(example);
    }

    @Override
    public Page<BookingsBusinessListVO> selectPage(BookingsBusinessQuery bookingsBusinessQuery, String siteId) {
        Example example = buildExample(bookingsBusinessQuery, siteId);
        Page<BookingsBusinessDO> page = bookingsBusinessDAO.selectPageByExample(example,
                bookingsBusinessQuery.getPageNo(),
                bookingsBusinessQuery.getPageSize());

        return page.to(BookingsBusinessListVO.class);
    }

    @Override
    public BookingsBusinessDO selectDOById(Long bookingsBusinessId, String siteId) {
        return listById(bookingsBusinessId, siteId);
    }

    @Override
    public List<BookingsBusinessDO> selectDO(BookingsBusinessQuery bookingsBusinessQuery, String siteId) {
        Example example = buildExample(bookingsBusinessQuery, siteId);
        return bookingsBusinessDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param bookingsBusinessQuery 查询参数
     * @param siteId                所属站点id
     * @return example
     */
    private Example buildExample(BookingsBusinessQuery bookingsBusinessQuery, String siteId) {
        Example example = new Example(BookingsBusinessDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, bookingsBusinessQuery.getOrders());
        return example;
    }
}




